'''
Description: 管理飞船行为文件
Author: gn
Date: 2024-04-19 13:17:06
LastEditors: sevenFunuction 1593215788@qq.com
LastEditTime: 2024-06-24 22:47:32
'''
import pygame
import logging

class Ship():
    '''管理飞船的行为'''

    def __init__(self, ai_game):
        
        #  vars 函数或 __dict__ 属性来获取对象的属性字典
        ai_game_attrs = vars(ai_game)
        # logging.info(ai_game_attrs)

        '''初始化飞船并设置其初始位置'''
        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()
        self.settings = ai_game.settings
        origin_image = pygame.image.load('alien_invasion/images/ship.png')
        
        # 改变飞船尺寸
        resize_image_width = origin_image.get_width() / 4
        resize_image_height = origin_image.get_height() / 4
        
        self.image = pygame.transform.scale(origin_image, (resize_image_width, resize_image_height))

        # 例：rect(5, 1030, 50, 50) 对应x: {x}, y: {y}, width: {width}, height: {height}
        self.rect = self.image.get_rect()
        
        
        self.moving_right = False
        self.moving_left = False
        
        # 将x坐标转换为浮点数，飞船移动更为丝滑
        self.x = float(self.rect.x)
        
        # 每艘新飞船放在画布中央
        self.rect.midbottom = self.screen_rect.midbottom
        
    def update(self):
      if self.moving_right and self.x < self.screen_rect.right - self.rect.width:
        self.x += self.settings.ship_speed
      if self.moving_left and self.x > -self.rect.width:
        # 防止飞船移动视口
        if self.x - self.settings.ship_speed > 0: self.x -= self.settings.ship_speed
        print(self.x)
      
      self.rect.x = self.x
        
    def blitme(self):
      # 指定位置绘飞船
      self.screen.blit(self.image, self.rect)